package com.zt.proxy.controller;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.zt.proxy.component.DependencyFactory;
import com.zt.proxy.dto.ResultDTO;
import com.zt.proxy.model.PomModel;
import com.zt.proxy.model.RegistryModel;
import com.zt.proxy.service.ZkAddressService;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/proxy/zk")
@Slf4j
public class ZkRegistryController {

    @Autowired
    private ZkAddressService zkAddressService;
    
    @Autowired
    private DependencyFactory dependencyFactory;

    @RequestMapping("/zkList")
    public String doListZk() {

        log.info("ZkRegistryController.zkList()");

        String result;

        try {

            List<RegistryModel> models = zkAddressService.listRegistry();
            result = JSON.toJSONString(models);

        } catch(Exception e) {

            result = "[]";
        }

        return result;
    }


    @RequestMapping("/doListRegistry")
    public ResultDTO<Object> doListRegistry() {

        log.info("ZkRegistryController.doListRegistry()");

        ResultDTO<Object> resultDTO = new ResultDTO<>();

        try {

            List<RegistryModel> models = zkAddressService.listRegistry();
            resultDTO.setData(models);
            resultDTO.setSuccess(true);

        } catch(Exception e) {

            resultDTO = ResultDTO.createExceptionResult("occur an error when list registry address : ", e, Object.class);
        }

        return resultDTO;
    }

    @RequestMapping("/addRegistry")
    public ResultDTO<RegistryModel> addRegistry(@NotNull RegistryModel dto) {

        log.info("ZkRegistryController.addRegistry({})", JSON.toJSONString(dto));

        ResultDTO<RegistryModel> resultDTO;

        try {

            resultDTO = zkAddressService.addRegistry(dto);

        } catch(Exception e) {

            resultDTO = ResultDTO.createExceptionResult(e, RegistryModel.class);
        }

        return resultDTO;
    }

    @RequestMapping("/delRegistry")
    public ResultDTO<RegistryModel> delRegistry(@NotNull RegistryModel dto) {

        log.info("ZkRegistryController.delRegistry({})", JSON.toJSONString(dto));

        ResultDTO<RegistryModel> resultDTO;

        try {

            resultDTO = zkAddressService.delRegistry(dto);

        } catch(Exception e) {

            resultDTO = ResultDTO.createExceptionResult(e, RegistryModel.class);
        }

        return resultDTO;
    }
    
    @RequestMapping("/testDownload")
    public String test(){
        PomModel pomModel = PomModel.builder().artifactId("pps-api").groupId("com.smy.pps").version("0.0.5-SNAPSHOT").build();
        try {
            dependencyFactory.downLoadMavenJar(pomModel );
        } catch (Exception e) {
            
            e.printStackTrace();
        }
        return "200";
    }
}
